package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.junit.*;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.CsConfirmReceiveReq;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.CsConfirmReceiveCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 客服确认收货 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_SqCsConfirmReceive {

	private static CsConfirmReceiveReq csConfirmReceiveReq;
	private static CsConfirmReceiveCall csConfirmreceivecall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("客服确认收货");
	}

	@Before
	public void caseUp() {

		csConfirmReceiveReq = new CsConfirmReceiveReq();
		csConfirmreceivecall = new CsConfirmReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(csConfirmreceivecall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 校验订单表状态，单主状态操作日志表
	 */
	public static void verifyOrderStatus(long orderId, String remark,
			long csId) {

		// 订单表(order)
		OrderPo ymtOrders = orderShardingWapper.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

		Logger.verifyEquals(4, ymtOrders.getOrderStatus(), "验证orderStatus");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(ymtOrders.getReceiveTime()) < 2,
				"验证数据库deliveryTime跟系统当前时间差为2分钟内");

		//订单主状态操作日志表(order_status_log)
		OrderStatusLogPo orderStatusLogPo = orderShardingWapper
				.selectOrderStatusLogByOrderId(orderId);

		Logger.comment("验证订单主状态操作日志表(order_status_log)");

		Logger.verifyEquals("[客服确认收货]," + remark, orderStatusLogPo.getMemo(), "验证数据库Memo");
		Logger.verifyEquals(4, orderStatusLogPo.getOrderStatus(),
				"验证数据库OrderStatus");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(orderStatusLogPo.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, orderStatusLogPo.getOrderId(),
				"验证数据库OrderId");
		Logger.verifyEquals(csId, orderStatusLogPo.getOperator(),
				"验证数据库Operator");

		AutoReceiveOptPo autoReceiveOptPo = orderShardingWapper
				.selectAutoReceiveOptByOrderId(orderId);

		Logger.verifyEquals(true, autoReceiveOptPo.getFinish(), "验证数据库finish");

	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CsConfirmReceive_010() {
		Logger.start(false, "订单号不存在");
		try {
			long orderId = 1111;
			
			csConfirmReceiveReq.setCsId(5352888);
			csConfirmReceiveReq.setOrderId(orderId);
			csConfirmReceiveReq.setRemark("收到了啊");
			
			// 发送请求
			csConfirmreceivecall.setData(csConfirmReceiveReq);
			csConfirmreceivecall.callService();
			
			Logger.verifyEquals(ECode.FAILED201, csConfirmreceivecall.getCode(),
					"验证Code");
			Logger.verifyEquals(String.format("订单号%s不存在", csConfirmReceiveReq.getOrderId()),
					csConfirmreceivecall.getString("msg"), "验证接口返回消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_CsConfirmReceive_011() {
		Logger.start(false, "订单号为null");
		try {
			csConfirmReceiveReq.setCsId(89756969);
			// confirmReceiveReq.setOrderId(orderId);
			csConfirmReceiveReq.setRemark("收到了啊");
			
			// 发送请求
			csConfirmreceivecall.setData(csConfirmReceiveReq);
			csConfirmreceivecall.callService();
			Logger.verifyEquals(ECode.ERROR400, csConfirmreceivecall.getCode(),
					"验证Code");
			Logger.verifyEquals("orderId:订单号未提供",
					csConfirmreceivecall.getString("msg"), "验证接口返回消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CsConfirmReceive_015() {
		Logger.start(true, "神器订单，确认收货成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = placeOrderResp.getUserId();
			long goodsOwnerId = placeOrderResp.getOrderList().get(0)
					.getOrderPo().getGoodsOwnerId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 神器订单发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					1L, null);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			// 确认收货请求
			csConfirmReceiveReq.setCsId(534235);
			csConfirmReceiveReq.setOrderId(orderId);
			csConfirmReceiveReq.setRemark("收到了啊");
			
			// 发送请求
			csConfirmreceivecall.setData(csConfirmReceiveReq);
			csConfirmreceivecall.callService();
			
			// 订单表(order)
			OrderPo ymtOrders = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

			Logger.verifyEquals(3, ymtOrders.getOrderStatus(), "验证orderStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
